package com.lin.csdn.controller;

import com.lin.csdn.service.impl.BlogService;
import com.lin.csdn.service.impl.BrowseService;
import com.lin.csdn.utils.BaseLogger;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet{
    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     *
     * code:
     * 0获取标题简介 GetTitleAndDescription
     * 2上传 OpenBlog
     * 3草稿标题简介 SaveTitleAndDescription
     * 4草稿正文 SaveArticle
     *
     * 获取文章调用的是BrowseServlet
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding ("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        //建立功能代表的code调用
        String GetTitleAndDescription="0";
        String OpenBlog="2";
        String SaveTitleAndDescription="3";
        String SaveArticle="4";
        //获取req数据
        String blogId=req.getParameter("blogId");
        String code=req.getParameter("code");

        List<Object> data;
        BlogService blogService=new BlogService();
        BrowseService browseService=new BrowseService();
        JSONObject object=new JSONObject();
        PrintWriter out=resp.getWriter();

        //根据需求调用各自的方法
        if(blogId.equals("0")&&(code.equals(SaveTitleAndDescription)||code.equals(SaveArticle))){
            String userName=req.getParameter("userName");
            String blogNewId=blogService.addBlog(userName);
            //当新建页面且运行了其中之一的保存草稿的办法，需要新建新的blog，并且返回新的id，用于后续的操作
            object.put("result",blogNewId);
            out.print(object);
        }
        else {
            if(blogId.equals("0")){
                object.put("result","wait");//未操作时返回等待信息，表示成功访问，但是未进行操作
                out.print(object);
            }
            else {
                if (code.equals(GetTitleAndDescription)) {
                    data = blogService.idBlog(blogId);
                    object.put("title", data.get(0));
                    object.put("description", data.get(1));
                    out.print(object);
                } else if (code.equals(OpenBlog)) {
                    String blogUrl = req.getParameter("searchPath");
                    blogService.updateStatus(blogId, blogUrl);
                    //将储存的数据作为文章改为开放状态
                    object.put("result", "上传成功");
                    out.print(object);
                } else if (code.equals(SaveTitleAndDescription)) {
                    String title = req.getParameter("title");
                    String description = req.getParameter("description");
                    String domain = req.getParameter("domain");
                    browseService.updateTdd(blogId, title, description, domain);
                    object.put("result", blogId);
                    out.print(object);
                } else if (code.equals(SaveArticle)) {
                    if (blogService.getArticleId(blogId)) {
                        //如果文章不存在，新增，存在则修改
                        blogService.addArticle(blogId);
                    }
                    String article = req.getParameter("article");
                    browseService.updateArticle(blogId, article);
                    object.put("result", blogId);
                    out.print(object);
                }
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
